module CompetitionsHelper
  def create_question_list(competition)
    question_list = QuestionList.new
    question_list.competition = competition
    question_list.section_id = params[:new].nil? ? competition.sections.first.id
      : params[:new]
    question_list
  end

  def tab_tag(text, tab)
    tab = 'general' if tab.nil? || tab == ''
    raw "<li>
      #{link_to text, tabs_competition_path(@competition, tab.to_s), 'class' =>
       "tab#{tab == get_tab_name_from_param ? ' selected' : ''}"}
    </li>"
  end

  # main
  def tab_content
    "competitions/#{get_tab_name_from_param}"
  end

  def get_tab_name_from_param
    s = params[:tab] || @competition_tab
    s = 'general' if s.nil?
    s = 'general' if !['general', 'question_lists', 'topics', 'tasks'].include? s
    s
  end

  #tasks
  def task_user_id_entry(user)
    task_user_id_entry_inner user.name_display, user.tasks_assigned_to.count, user.id.to_s
  end
  def task_user_id_entry_inner(name, count, id)
    content_tag(:li, 
      content_tag(:a,
        content_tag(:div, name, 'class' => 'left') + 
        content_tag(:div, count, 'class' => 'right') + 
        content_tag(:div, '', 'class' => 'clear'),
        onclick: 'return false'
      ),
      'class' => params[:user_id].to_s == id ? 'selected' : '',
      onclick: create_js({
        "set('user_id', '#{id}')" => 'form_ltaskcfilter',
        submit: 'form_ltaskcfilter'
    }))
  end
  def task_filter_button(text, filter)
    content_tag :a, text, 
      'class' => "button#{params[:filter].to_s == filter ? ' selected' : ''}",
      onclick: create_js({
        "set('filter', '#{filter}')" => 'form_ltaskcfilter',
        submit: 'form_ltaskcfilter'
      })
  end
  def task_scope_display
    parent = Task.all
    parent = params[:user_id].nil? || params[:user_id] == '' ?
      parent :
      parent.where(assigned_to_id: params[:user_id])
    scope = (params[:filter].nil? || params[:filter] == '' ? 'all' : params[:filter]).to_sym
    if ![:all, :finished, :unfinished, :unspecified].include? scope
      scope = :all
    end
    parent.send(scope)
  end

  #question_lists
  def question_list_section_id_entry(section)
    question_list_section_id_entry_inner section.name,
      section.question_lists.count, section.id.to_s
  end
  def question_list_section_id_entry_inner(name, count, id)
    content_tag(:li, 
      content_tag(:a,
        content_tag(:div, name, 'class' => 'left') + 
        #content_tag(:div, count, 'class' => 'right', 'style' => 'display: none') + 
        content_tag(:div,
          (id == '' ? count : content_tag(:a, count,
            href: tabs_competition_path(@competition, 'question_lists', 'new' => id),
            style: 'padding: 0 4px'
            )),
          'class' => "right#{id == '' ? '' : ' mimibutton-append'}",
          style: id == '' ? 'padding: 0 4px;margin-right:-4px' : 'margin-right:-4px') + 
        content_tag(:div, '', 'class' => 'clear'),
        onclick: 'return false'
      ),
      'class' => params[:section_id].to_s == id ? 'selected' : '',
      onclick: create_js_true({
        "set('section_id', '#{id}')" => 'form_lqlcfilter',
        submit: 'form_lqlcfilter'
    }))
  end

  def question_list_scope_display
    params[:section_id].nil? || params[:section_id] == '' ? @competition.question_lists :
      @competition.question_lists.where(section_id: params[:section_id])
  end

  #topics
  def topic_tag_entry(text, id)
    (params[:tag_id].nil? ? '' : params[:tag_id]) == id ? text : 
    content_tag(:a, text,
      onclick: create_js({
        "set('tag_id','#{id}')" => 'form_ltcfilter',
        submit: 'form_ltcfilter'
      }))
  end

  def topic_scope_display
    params[:tag_id].nil? || params[:tag_id] == '' ? @competition.topics :
      @competition.topics.where(tag_ids: params[:tag_id])
  end
end
